package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lyd
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2023-08-15 13:49:57
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfo(SaveSpuVo vo) {
        //1.向spu_info表存数据
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(vo, spuInfo);
        this.save(spuInfo);

        //2.向spu_image表存数据
        Long spuId = spuInfo.getId();
        List<SpuImage> spuImageList = vo.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    spuImage.setSpuId(spuId);
                    spuImage.setImgName(item.getImgName());
                    spuImage.setImgUrl(item.getImgUrl());
                    return spuImage;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3.向spu_sale_attr表存数据
        List<SpuSaleAttr> spuSaleAttrs = vo.getSpuSaleAttrList().stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    spuSaleAttr.setSpuId(spuId);
                    spuSaleAttr.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    spuSaleAttr.setSaleAttrName(item.getSaleAttrName());
                    return spuSaleAttr;
                }).collect(Collectors.toList());

        spuSaleAttrService.saveBatch(spuSaleAttrs);

        //4.向spu_sale_attr_value表存数据
        List<SpuSaleAttrValue> spuSaleAttrValueList = vo.getSpuSaleAttrList().stream()
                .flatMap(item -> item.getSpuSaleAttrValueList().stream()
                        .map(i -> {
                            i.setSaleAttrName(item.getSaleAttrName());
                            return i;
                        }))
                .map(item -> {
                    SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                    spuSaleAttrValue.setSpuId(spuId);
                    spuSaleAttrValue.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    spuSaleAttrValue.setSaleAttrValueName(item.getSaleAttrValueName());
                    spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                    return spuSaleAttrValue;
                }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);

    }
}




